package lgkj.meicqt20.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import lgkj.meicqt20.R;
import lgkj.meicqt20.utils.CommUtils;

/**
 * meicqt.lgkj.com.meicqt.view Created by hjx on 2017/4/28 15:55
 *
 * @Company:
 * @Description: ${TODO}
 * @Email:1263276841@qq.com
 * @Version $Rev$
 * @UpdateAuthor $Author$ on 2017/4/28$
 * @UpdateDes ${TODO}
 */

public class BannerViewpager extends ViewPager {
    private String TAG = "BannerViewpager";
    //view
    private static AppCompatActivity mAty;
    //adapter
    private BannerPagerAdapter mBannerPagerAdapter;
    //data
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    //controller
    private int mCurPos;
    private int mTotalSize = -1;

    private Timer mTimer;
    private TimerTask mTimerTask;
    //constant
    private final int mPeriod = 15000;
    private String mDtag = getClass().getSimpleName();
    private int mType;


    public BannerViewpager(Context context) {
        this(context, null);
    }

    public BannerViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.BannerType);
        mType = t.getInteger(R.styleable.BannerType_type, 0);
        t.recycle();
        mAty = (AppCompatActivity) context;
        init();
    }

    private void init() {
        mBannerPagerAdapter = new BannerPagerAdapter(mAty.getSupportFragmentManager());
        setAdapter(mBannerPagerAdapter);
        mTimer = new Timer();
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        super.setAdapter(adapter);
        mTotalSize = getAdapter().getCount();
        if (mTotalSize > 0) {
            resetTimerTask();
            mTimer.schedule(mTimerTask, mPeriod, mPeriod);
        }

    }

    /**
     * 设置数据
     *
     * @param mListUrls 图片地址
     */
    public void setData(List<String> mListUrls) {
        if (mListUrls.size() > 0)
            mFragmentList.clear();
        mCurPos = 0;
        for (int i = 0; i < mListUrls.size(); i++) {
//            Log.i("zzm", " 廣告：  " + i + ",地址：" + mListUrls.get(i));
            BannerFragment f = new BannerFragment();
            Bundle b = new Bundle();
            b.putString(BannerFragment.picPath, mListUrls.get(i));
            b.putInt("type", mType);
            f.setArguments(b);
            mFragmentList.add(f);
        }
        mBannerPagerAdapter = new BannerPagerAdapter(mAty.getSupportFragmentManager());
        setAdapter(mBannerPagerAdapter);
        mBannerPagerAdapter.notifyDataSetChanged();
    }


    public Timer getTimer() {
        if (mTimer == null) {
            throw new NullPointerException("u must setAdapter in this");
        }
        return mTimer;
    }

    private void resetTimerTask() {
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                ++mCurPos;
                if (mCurPos == mTotalSize) {
                    mCurPos = mCurPos % mTotalSize;
                }
                //UIThread
                ((AppCompatActivity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setCurrentItem(mCurPos);
                    }
                });
            }
        };
    }

    public static class BannerFragment extends Fragment {
        //view
        private ImageView iv_banner_bg;

        //data
        private String picUrl;
        private int mType;

        //controller

        //constant
        public static final String picPath = "picPath";
        private String TAG = "BannerFragment";

        public BannerFragment() {
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            iv_banner_bg = new ImageView(getContext());
            iv_banner_bg.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            iv_banner_bg.setScaleType(ImageView.ScaleType.FIT_XY);
            picUrl = getArguments().getString(picPath);
            mType = getArguments().getInt("type");
            if (mType == 0) {
                CommUtils.LoadImage(mAty, iv_banner_bg, picUrl, R.mipmap.commo_banner_vertical);
            } else {
                CommUtils.LoadImage(mAty, iv_banner_bg, picUrl, R.mipmap.commo_banner_horizontal);
            }
            Log.d(TAG, "mType:" + mType + ",picUrl:" + picUrl);
            return iv_banner_bg;
        }
    }

    class BannerPagerAdapter extends FragmentPagerAdapter {

        public BannerPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}

